﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace DAO
{
    public class DonDatHangDAO : DataAccess
    {
        public DonDatHang CreateObj(DataRow row)
        {
            DonDatHang obj = new DonDatHang();
            obj.MaDDH = Utils.ToStr(row["MaDDH"]);
            obj.NgayDat = Utils.ToDataTime(row["NgayDat"]);
            //obj.TongTien = Utils.ToFloat(row["TongTien"]);
            obj.NguoiLap = Utils.ToStr(row["NguoiLap"]);
            obj.PhuongThucTT = Utils.ToInt(row["PhuongThucTT"]);
            obj.SubSP = Utils.ToFloat(row["SubSP"]);
            obj.SubTL = Utils.ToFloat(row["SubTL"]);
            obj.Thue = Utils.ToFloat(row["Thue"]);
            obj.ChietKhau = Utils.ToFloat(row["ChietKhau"]);
            return obj;
        }
        public List<DonDatHang> GetList()
        {
            DataTable tb = GetData("EXEC GetDonDatHang");
            List<DonDatHang> Lst = new List<DonDatHang>();
            for (int i = 0; i < tb.Rows.Count; i++)
                Lst.Add(CreateObj(tb.Rows[i]));
            return Lst;
        }
        public bool Delete(DonDatHang obj)
        {
            return UpdateDeleteData("DeleteDonDatHang ?", CommandType.Text, "@MaDDH", obj.MaDDH, OleDbType.VarChar);
        }
        public float GetChietKhau()
        {
            return Utils.ToFloat(GetData("SELECT dbo.ChietKhau()").Rows[0][0]);
        }
        public float GetVAT()
        {
            return Utils.ToFloat(GetData("SELECT dbo.Thue()").Rows[0][0]);
        }
        public bool Insert(DonDatHang obj)
        {
            ChiTietDonDatHangDAO ct = new ChiTietDonDatHangDAO();
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine("DECLARE @ID VARCHAR(50) = NEWID()");
            SQL.AppendLine("INSERT INTO DonDatHang(MaDDH, NgayDat, TongTien, NguoiLap, PhuongThucTT, TinhTrang)");
            SQL.AppendLine("VALUES(@ID, ?, ?, ?, ?, 0)");
            SQL.AppendLine("SELECT @ID");
            bool tmp = false;
            Conntection.Open();
            OleDbTransaction tran = Conntection.BeginTransaction();
            try
            {
                OleDbCommand cmd = new OleDbCommand(SQL.ToString(), Conntection, tran);
                cmd.Parameters.Add("@NgayDat", OleDbType.Date).Value = obj.NgayDat;
                cmd.Parameters.Add("@TongTien", OleDbType.Double).Value = obj.TongTien;
                cmd.Parameters.Add("@NguoiLap", OleDbType.VarChar).Value = obj.NguoiLap;
                cmd.Parameters.Add("@PhuongThucTT", OleDbType.Integer).Value = obj.PhuongThucTT;
                obj.MaDDH = Utils.ToStr(cmd.ExecuteScalar());
                for (int i = 0; i < obj.LstChiTietDonDatHang.Count; i++)
                {
                    obj.LstChiTietDonDatHang[i].MaDDH = obj.MaDDH;
                    if (!ct.Insert(obj.LstChiTietDonDatHang[i], Conntection, tran))
                        throw new InvalidProgramException();
                }
                tran.Commit();
                tmp = true;
            }
            catch (System.Exception ex)
            {
                tran.Rollback();
                tmp = false;
            }
            Conntection.Close();
            return tmp;
        }
        public bool Update(DonDatHang obj)
        {
            ChiTietDonDatHangDAO ct = new ChiTietDonDatHangDAO();
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine("UPDATE DonDatHang");
            SQL.AppendLine("SET NgayDat = ?,");
            //SQL.AppendLine("TongTien=?");
            SQL.AppendLine("NguoiLap = ?,");
            SQL.AppendLine("PhuongThucTT = ?");
            SQL.AppendLine("WHERE MaDDH = ?");
            SQL.AppendLine();
            SQL.AppendLine("DELETE ChiTietDonDatHang");
            SQL.AppendLine("WHERE MaDDH = ?");
            bool tmp = false;
            Conntection.Open();
            OleDbTransaction tran = Conntection.BeginTransaction();
            try
            {
                OleDbCommand cmd = new OleDbCommand(SQL.ToString(), Conntection, tran);
                cmd.Parameters.Add("@NgayDat", OleDbType.Date).Value = obj.NgayDat;
                cmd.Parameters.Add("@NguoiLap", OleDbType.VarChar).Value = obj.NguoiLap;
                cmd.Parameters.Add("@PhuongThucTT", OleDbType.Integer).Value = obj.PhuongThucTT;
                cmd.Parameters.Add("@MaDDH", OleDbType.VarChar).Value = obj.MaDDH;
                cmd.Parameters.Add("@MaDDHX", OleDbType.VarChar).Value = obj.MaDDH;
                cmd.ExecuteNonQuery();
                for (int i = 0; i < obj.LstChiTietDonDatHang.Count; i++ )
                {
                    obj.LstChiTietDonDatHang[i].MaDDH = obj.MaDDH;
                    if (!ct.Insert(obj.LstChiTietDonDatHang[i], Conntection, tran))
                        throw new InvalidProgramException();
                }
                tran.Commit();
                tmp = true;
            }
            catch (System.Exception ex)
            {
                tran.Rollback();
                tmp = false;
            }
            Conntection.Close();
            return tmp;
        }
    }
}
